<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1024" destroy-on-close @closed="$emit('closed')">
		<!-- <sc-water-mark ref="wm" text="已审核" subtext="誉卓纱线"> -->
		<sk-tabs type="border-card">
			<el-tab-pane ref="tab" label="基础信息">
				<el-form :model="form" :rules="rules" ref="dialogForm" label-width="70px" label-position="right">
					<el-row align="bottom">
						<el-col :span="7" align="bottom">
							<el-form-item label="单据日期" prop="billdate">
								<el-date-picker v-model="form.billdate" type="date" placeholder="选择日期" format="YYYY-MM-DD"
									value-format="YYYY-MM-DD" />
								<!-- <el-input label="单据日期" v-model="form.code" clearable></el-input> -->
							</el-form-item>
						</el-col>
						<el-col :span="7" align="bottom">
							<el-form-item label="编号" prop="code">
								<el-input label="编号" v-model="form.code" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="7">
							<el-form-item label="备注" prop="remark">
								<el-input v-model="form.remark" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="7">
							<el-form-item label="派车单" prop="car_id">
								<skAutoChoose v-model="form.car.code" :name="'car'" :assign="form" @select="selectCar">
								</skAutoChoose>
							</el-form-item>
						</el-col>
						<el-col :span="7">
							<el-form-item label="运输商" prop="car.company_id">
								<skAutoChoose v-model="form.car.company.name" :name="'factory'" :asKey="'company_id'"
									:assign="form.car" :params="[{ category_id: 54 }]">
								</skAutoChoose>
							</el-form-item>
						</el-col>
						<el-col :span="7">
							<el-form-item label="运费" prop="car.amount">
								<el-input v-model="form.car.amount" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="7">
							<el-form-item label="搬运单" prop="carry_id">
								<skAutoChoose v-model="form.carry.code" :name="'carry'" :assign="form"
									@select="selectCarry">
								</skAutoChoose>
							</el-form-item>
						</el-col>
						<el-col :span="7">
							<el-form-item label="搬运人" prop="carry.company_id">
								<skAutoChoose v-model="form.carry.company.name" :name="'factory'" :asKey="'company_id'" :assign="form.carry"
									:params="[{ category_id: 55 }]">
								</skAutoChoose>
							</el-form-item>
						</el-col>
						<el-col :span="7">
							<el-form-item label="搬运费" prop="carry.amount">
								<el-input v-model="form.carry.amount" clearable></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
				<el-divider content-position="left">库存信息</el-divider>
				<sc-VxeTable ref="save" :gridOps="gridOps" :data="form.details" :gridEvts="gridEvts"
					:tablecode="'storage_action_deptallocation_save'">
				</sc-VxeTable>
			</el-tab-pane>
			<el-tab-pane label="附件">
				<sk-upload v-model="form.attachments" :limit="10" draggable tip="单个文件不要超过10M,请上传图像格式文件">
				</sk-upload>

				<el-dialog v-model="dialogVisible">
					<img w-full :src="dialogImageUrl" alt="Preview Image" />
				</el-dialog>
			</el-tab-pane>
		</sk-tabs>
		<!-- </sc-water-mark> -->
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
	<skBrowserChoose v-model="openChooseDlg" :name="'storage_browser_all'" @close="openChooseDlg = false" @select="additems">
	</skBrowserChoose>
</template>

<script>
import skBrowserChoose from "@/components/skBrowserChoose";
import skAutoChoose from "@/components/skAutoChoose";
import XEUtils from "xe-utils";
import tool from "@/utils/tool";
export default {
	emits: ["success", "closed"],
	components: { skBrowserChoose, skAutoChoose },
	data() {
		return {
			mode: "add",
			dialogImageUrl: "",
			dialogVisible: false,
			openChooseDlg: false,
			gridOps: {
				columns: [
					{
						field: "auxiliary_quantity",
						event: {
							keyup: ({ row }) => {
								row["quantity"] =
									row["auxiliary_quantity"] * 25;
							},
						},
					},
				],
				proxyConfig: null,
				height: 300,
			},
			gridEvts: {
				choose: () => (this.openChooseDlg = true),
			},
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			form: {
				id: "",
				billdate: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
				code: "",
				remark: "",
				details: [],
				attachments: [],
				car: { company: {} },
				carry: { company: {} },
			},
			rules: { billdate: { required: true, message: "请选择单据日期" } },
			visible: false,
			isSaveing: false,
		};
	},
	mounted() { },
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		selectCar(data) {
			if (typeof data.code == "undefined") {
				data = { company: {} };
			}
			this.form.car = data;
		},
		selectCarry(data) {
			if (typeof data.code == "undefined") {
				data = { company: {} };
			}
			this.form.carry = data;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.storage.deptallocation.save.post(
						this.form
					);
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit("success", this.form, this.mode);
						this.visible = false;
						this.$message.success("操作成功");
					}
				}
			});
		},
		async getItems(id) {
			var res = await this.$API.storage.deptallocation.info.get({
				filter: [{ id: id }],
			});
			if (res.code == 200) {
				Object.assign(this.form, tool.removeNull(res.data));
			}
		},
		additems(items) {
			this.openChooseDlg = false;
			let department_id = null;
			items.map((defrow) => {
				let row = this.$TOOL.objCopy(defrow);
				if (department_id == null) {
					department_id = row.location.department_id;
				} else if (department_id != row.location.department_id) {
					return this.$message.error("一次只能从一个部门要货！");
				}
				row["oldlocation"] = row.location;
				row["oldlocation_id"] = row.location_id;
				row["quantity"] = parseFloat(row.available_quantity);
				row["auxiliary_quantity"] = parseFloat(
					row.available_auxiliary_quantity
				);
				row.id = "key" + row.id;
				delete row.location_id;
				delete row.location;
				this.form.details.push(row);
			});
		},
		//表单注入数据
		setData(data) {
			this.getItems(data.id);
		},
	},
};
</script>

<style></style>
